<?php
namespace app\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CorpMaster extends Model
{
    public $corp_id = "";
    public $corp_name = "";
    public $corp_catelogue = "";
    public $corp_postcode = "";
    public $corp_adress = "";
    public $corp_tel = "";
    public $corp_fax = "";
    public $corp_mail = "";
    public $corp_stuff = "";
    public $stuff_kana = "";
    public $stuff_mail = "";
    public $corp_stuff_telephone = "";
    public $corp_stuff_kana = "";
    public $corp_stuff_mail = "";
    public $price_rate = "";
    public $remark = "";
    public $delete_flg = "";
    public $created_at = "";
    public $updated_at = "";
    public $buil_name = "";
    public $bill_method = "";
    // 设置表名NAME="corp_master";
    // 设置表名
    const TABLE_NAME = "corp_master";

    protected $table = 'corp_master';

    protected $primaryKey = 'corp_id';

    protected $CorpMaster = self::TABLE_NAME;

    public $timestamps = false;

    public function readCorpMaster() // 查
    {
        return $this->all();
    }

    public function oneCorpMaster($data, $arr) // 单条查询
    {
        return $this->where($data, $arr)
            ->get()
            ->toArray();
    }

    public function delCorpMaster($corp_id) // 删
    {
        $sql = 'update corp_master set delete_flg = 1 where corp_id = ?';
        $param = [
            $corp_id
        ];
        $delflag = DB::update($sql, $param);
        return $delflag;
    }

    public function updUserPass($password, $user_id) // 删
    {
        $sql = 'update user_master set password = ? where user_id = ?';
        $param = [
            $password,
            $user_id
        ];
        $delflag = DB::update($sql, $param);
        return $delflag;
    }

    public function updCorpMaster($data, $list, $arr) // 改
    {
        $CorpMaster = $this->where($list, '=', $data);
        return $CorpMaster->update($arr);
    }

    public function addCorpMaster($data) // 增
    {
        return $this->insertGetId($data);
    }

    public function getPriceGroup()
    {
        $sql = 'SELECT DISTINCT group_id FROM price_setting_master';
        $discMaster = DB::select($sql);
        return $discMaster;
    }

    public function getCorpInfoByCorpId($compId)
    {
        $sql = 'select * from corp_master where corp_id = ? and delete_flg = 0';
        $param = [
            $compId
        ];

        $corpInfo = DB::select($sql, $param);
        return $corpInfo;
    }

    public function getCorpMaster()
    {
        $sql = 'select * from corp_master where delete_flg = 0';

        $corpInfo = DB::select($sql);
        return $corpInfo;
    }

    public function getCorpNameCorpMaster($corpId)
    {
        $sql = 'select corp_name from corp_master where corp_id = ? and delete_flg = 0';

        $param = [
            $corpId
        ];

        $corpInfo = DB::select($sql, $param);
        return $corpInfo;
    }

    public function addCsvCorpMaster($data)
    {
        if (isset($data[0]) && ! empty($data[0])) {
            $sql = 'INSERT INTO  corp_master (corp_name, corp_catelogue, corp_postcode, corp_adress, corp_tel, corp_fax, corp_mail, corp_stuff, stuff_kana, stuff_mail, corp_stuff_telephone, corp_stuff_kana, corp_stuff_mail, price_rate, remark, delete_flg, created_at, updated_at, buil_name)
                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
            DB::insert($sql, $data);
        }
    }
}
